/* jslint unused: false */
/* global define, $, console, alert */
define(function(require) {

    'use strict';

    var _ = require('underscore'),
        Backbone = require('backbone'),
        Base = require('components/pop/base'),
        tpl = require('text!components/pop/templates/send_goods.html'),
        Utils = require('core/utils'),
        Select2 = require('select2');

    require('jqueryui');

    return Base.extend({
        template: _.template(tpl),
        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback',
            'change .js-express': 'toggleForm',
            'keydown .js-text-placeholder': function(e) {
                if (e.keyCode === Utils.keyCode.ENTER) {
                    this.triggerCallback();
                }
            }
        },
        onRender: function() {
            console.log(this.$('.js-company'));

            $.extend($.fn.select2.defaults, {
                formatNoMatches: function() {
                    return '找不到相关的物流公司~喵。';
                }
            });
            this.$('.js-company').select2({
                placeholder: '请选择一个物流公司',
                width: 220
            });
        },
        toggleForm: function(e) {
            var $tar = $(e.target);
            if (+$tar.val() === 1) {
                this.$('.js-express-section').hide();
            } else {
                this.$('.js-express-section').show();
            }

        },
        positioning: function() {
            var view = this;
            var className = view.options.className;

            view.$el.show();
            if (className.indexOf('left') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'right center',
                    at: 'left center',
                    collision: 'none'
                });
            } else if (className.indexOf('bottom') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'center top',
                    at: 'center bottom',
                    collision: 'none'
                });
            } else if (className.indexOf('top') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'center bottom',
                    at: 'center top',
                    collision: 'none'
                });
            } else {
                view.$el.position({
                    of: view.target,
                    my: 'left center',
                    at: 'right center',
                    collision: 'none'
                });
            }

            view.el.className = view.options.className + ' popover-goods';
        },
        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.positioning();
            this.clearInput();
            this.show();
        },
        triggerCallback: function() {
            var no = $.trim(this.$('.js-number').val()),
                id = Number($.trim(this.$('.js-company').select2('val'))),
                no_express = this.$('.js-express:checked').val();

            if (+no_express !== 1) {
                if (!no) {
                    Utils.errorNotify('快递单号不能为空');
                    return;
                }
                if (!id) {
                    Utils.errorNotify('请选择一个物流公司');
                    return;
                }
            }

            this.callback({
                no: no,
                id: id,
                no_express: no_express
            });
            this.hide();
        },
        clearInput: function() {
            this.$('.js-number').val('');
        }
    });
});
